<div class="layui-fluid ">
	<div class="layui-col-md6">
		<form class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">旧密码</label>
				<div class="layui-input-block">
					<input type="password" name="oldPassword" required lay-verify="required" placeholder="请输入旧密码"
						autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">新密码</label>
				<div class="layui-input-block">
					<input type="password" name="newPassword" required lay-verify="required" placeholder="请输入新密码"
						autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">确认新密码</label>
				<div class="layui-input-block">
					<input type="password" name="newPasswordConfirm" required lay-verify="required"
						placeholder="请再次输入新密码" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="button" class="layui-btn" lay-submit lay-filter="update-password">确认修改</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
	layui.use(["table", "layer", "form"], function (table, layer, form) {
		//修改密码
		form.on('submit(update-password)', function (data) {
			var params = data.field;
			var userId = parseInt(instance.defaults.userId);
			var oldPassword = params.oldPassword;
			var newPassword = params.newPassword;
			var newPasswordConfirm = params.newPasswordConfirm;
			//判断旧密码和新密码是否一样
			if (oldPassword == newPassword) {
				layer.confirm("新密码不能与最近设置的密码相同!", function (index) {
					resetForm();
					//关闭弹窗
					layer.close(index)
				})
			} else
				//判断两次密码是否一致
				if (newPassword != newPasswordConfirm) {
					layer.confirm("两次密码不一致!", function (index) {
						resetForm();
						//关闭弹窗
						layer.close(index)
					})
				}
			//当所有的条件满足才会发送axios请求
			if (oldPassword != newPasswordConfirm && oldPassword != newPassword && newPassword == newPasswordConfirm) {
				//axios发送请求修改密码
				axios.post(serverPath + "/users/" + userId,
					{
						oldPwd: oldPassword,
						newPwd: newPassword,
						newPwdConfirm: newPasswordConfirm
					},
					{
						headers: {
							"auth-token": getToken()
						}
					})
					.then(function (response) {
						resetForm();
						layer.alert(response.data.data)
					})
					.catch(function (error) {
						layer.alert(error.response.data.error)
					});
			}
			return false;
		});

		//封装重置表单
		function resetForm() {
			form.val("form-updatePwd", {
				"oldPassword": "",
				"newPassword": "",
				"newPasswordConfirm": ""
			});
		}
	});
</script>